//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Common
{
    using System;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;

    /// <summary>
    /// Custom exception handler takes the Action object as argument and executes inside the try catch block and handles the exception based on the policy.
    /// </summary>
    public static class ExceptionHandler
    {
        /// <summary>
        /// Executes the <paramref name="action"/> object inside the said exception policy.
        /// </summary>
        /// <param name="policyName">Enterprise library exception handling policy name.</param>
        /// <param name="action">Instance of the Action.</param>
        public static void Handle(string policyName, Action action)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, policyName);
                if (rethrow)
                {
                    throw;
                }
            }
        }

        /// <summary>
        /// Executes the <paramref name="action"/> object inside the said exception policy.
        /// It takes the Action object which needs to be executed inside the finally block.
        /// </summary>
        /// <param name="policyName">Enterprise library exception handling policy name.</param>
        /// <param name="action">Instance of the Action.</param>
        /// <param name="finalBlock">Action to be executed inside the finally block.</param>
        public static void Handle(string policyName, Action action, Action finalBlock)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, policyName);
                if (rethrow)
                {
                    throw;
                }
            }
            finally
            {
                finalBlock();
            }
        }

        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="policyName">Enterprise library exception handling policy name.</param>
        /// <param name="exception">The exception which needs to be handled.</param>
        public static void Handle(string policyName, Exception exception)
        {
            ExceptionPolicy.HandleException(exception, policyName);
        }
    }
}
